﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;


namespace BACKOFFICE
{
    class StaticProperties
    {
        public static string LocalIPAddress { get; set; }
        public static double TimeToUpdate { get; set; }
        public static int ReceiptNo { get; set; }
        public static string TableName { get; set; }
        public static string ServerName { get; set; }
        public static int ServerID { get; set; }
        public static List<string> EmployeeNamesList = new List<string>();

        public static int StartingReceiptNo { get; set; }

        public static string ReceiptNoGenerator()
        {
            DateTime date = DateTime.Now;
            double datetoOADate = date.ToOADate();


            int i = (int)Math.Truncate(date.ToOADate());

            string ReceiptNumberCombo = i.ToString() + StartingReceiptNo.ToString();
            StartingReceiptNo++;

            return ReceiptNumberCombo;
        }

        public static class StartupStuff
        {
            public static string FileLocation { get; set; }
            public static string FileName = "Startup.xml";

            public static Process theRunningProc;

            public static void CreateStartupXMLFile()
            {
                using (XmlWriter writer = XmlWriter.Create("Startup.xml"))
                {
                    writer.WriteStartElement("Startup");
                    writer.WriteElementString("SQLServerIP", "192.168.1.13");
                    writer.WriteElementString("SQLServerPORT", "51274");
                    //writer.WriteElementString("publisher", "Addison-Wesley");
                    //writer.WriteElementString("price", "64.95");
                    writer.WriteEndElement();
                    writer.Flush();
                }
            }

            public static void ReadStartupXMLFile()
            {

            }
            
        }

        public static string GetLocalIP()
        {
            string _IP = null;

            // Resolves a host name or IP address to an IPHostEntry instance.
            // IPHostEntry - Provides a container class for Internet host address information. 
            System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

            // IPAddress class contains the address of a computer on an IP network. 
            foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
            {
                // InterNetwork indicates that an IP version 4 address is expected 
                // when a Socket connects to an endpoint
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    _IP = _IPAddress.ToString();
                }
            }
            return _IP;
        }

        public static bool checkLogin(int UserName, int Password)
        {
            foreach (DataRow row in DataTableClass.EmployeeLogin.Rows) // Loop over the rows.
            {
                if ((row["EmployeeID"].ToString().Trim() == UserName.ToString()) && (row["EmployeePassword"].ToString().Trim() == Password.ToString()))
                {

                    ServerName = row["EmployeeFullName"].ToString().Trim();
                    ServerID = (int)row["EmployeeID"];

                    return true;
                }
            }

            return false;
        }

        public static int GetEmployeeID(string EmployeeName)
        {
            foreach (DataRow row in DataTableClass.EmployeeLogin.Rows) // Loop over the rows.
            {
                if (row["EmployeeName"].ToString().Trim() == EmployeeName)
                {
                    ServerName = row["EmployeeName"].ToString().Trim();
                    ServerID = (int)row["EmployeeID"];

                    return (int)row["EmployeeID"];
                }
            }

            return 0;
        }

        public static void GetEmployeeNameList()
        {
            EmployeeNamesList.Clear();

            foreach (DataRow row in DataTableClass.EmployeeLogin.Rows) // Loop over the rows.
            {
                EmployeeNamesList.Add((row["EmployeeFullName"].ToString().Trim()));
            }
        }
    }
}
